#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4005,M=60005;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-48,ch=getchar();}
	return x*f;
}
bool SSS;

int prm[M],tot,cnt;
bool vis[M];
void pre(){
    for(int i=2;i<=60000;i++){
        if(!vis[i]){
            prm[++tot]=i;
            vis[i]=1;
        }
        for(int j=1;j<=tot&&i*prm[j]<=60000;j++){
            vis[i*prm[j]]=1;
            if(i%prm[j]==0) break;
        }
    }
    for(int i=2;i<=60000;i++) vis[i]&=(i%20==3||i<=10);
    return;
}

int T,n,m,D[N][N],val[N];
int X[N],Y[N];

bool TTT;//Lim:512Mib

signed main(){
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
    pre();
    T=read();
    while(T--){
        n=read(),m=read();
        for(int i=1;i<=n;i++) X[i]=read(),Y[i]=read();
        for(int i=1;i<=n;i++) val[i]=read();
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
                D[i][j]=D[j][i]=abs(X[i]-X[j])+abs(Y[i]-Y[j]);
        int res=0;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                for(int k=j+1;k<=n;k++){
                    int x=D[i][j]+D[i][k]+D[j][k]-max(D[i][j],max(D[i][k],D[j][k]))-min(D[i][j],min(D[i][k],D[j][k]));
                    if(vis[x]) res+=(val[i]+val[j]+val[k]);
                }
            }
        }
        printf("%lld\n",res);
    }
	return 0;
}